package com.jgp.third.wx.controller;

import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.common.annotation.UIParam;
import com.jgp.third.wx.model.WxConfig;
import com.jgp.third.wx.model.WxUseFor;
import com.jgp.third.wx.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/6/3
 */
@RestController
@RequestMapping("third/wx/wxConfigApiController")
public class WxConfigApiController extends JGPBackController {

    @Autowired
    private WxService wxService;

    @RequestMapping("/queryUseFor")
    public Result queryUseFor(String name, @UIParam("pager") Pager pager) {
        List<WxUseFor> list = wxService.queryUseFor(name,pager);
        List<GridResult.Category> cats = new ArrayList<>();
        cats.add(new GridResult.Category("用途", "name"));
        cats.add(new GridResult.Category("控制器", "path"));
        cats.add(new GridResult.Category("描述", "remark"));
        return ajaxReGrid("gdata", cats, list, pager);
    }
    
    @RequestMapping("/saveUseFor")
    public Result saveUseFor(WxUseFor useFor, HttpServletRequest request) {
        String toAuth = request.getScheme() + "://" + useFor.getDomain() + request.getContextPath() +
                "/third/wx/wxInterfaceController/toAuth?useForId="+useFor.getId()+"&domain="+useFor.getDomain();
        useFor.setShareLink(toAuth);
        wxService.saveUseFor(useFor);
        return ajaxRe(true);
    }

    @RequestMapping("/queryConfigs")
    public Result queryAllConfig(String name,@UIParam("pager") Pager pager) {
        List<GridResult.Category> cats = new ArrayList<>();
        cats.add(new GridResult.Category("配置名", "name"));
        cats.add(new GridResult.Category("appId", "appId"));
        List<WxConfig> list = wxService.queryConfig(name,pager);
        return ajaxReGrid("gdata", cats, list, pager);
    }

    @RequestMapping("/saveConfig")
    public Result saveConfig(WxConfig config) {
        wxService.saveConfig(config);
        return ajaxRe(true);
    }

    @RequestMapping("/removeConfig")
    public Result removeConfig(Long id) {
        wxService.removeConfig(id);
        return ajaxRe(true);
    }

    @RequestMapping("/removeUseFor")
    public Result removeUseFor(Long useForId) {
        wxService.removeUseFor(useForId);
        return ajaxRe(true);
    }

    @RequestMapping("/bindConfigToUseFor")
    public Result bindConfigToUseFor(Long useForId,Long configId) {
        wxService.bindConfigToUseFor(useForId,configId);
        return ajaxRe(true);
    }

}
